﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ASPItalia.ModelVirtualCasting.Common;
using ASPItalia.ModelVirtualCasting.ObjectModel;
using ASPItalia.ModelVirtualCasting.Web.Models;

namespace ASPItalia.ModelVirtualCasting.Web.Controllers
{
    public class PersonController : Controller
    {
        private IPersonRepository personRepository;

        internal PersonController(IPersonRepository personRepository)
        {
            this.personRepository = personRepository;
        }

        public PersonController()
            : this (ApplicationContext.Current.People)
        { }

        public ActionResult Profile(string nickname)
        {
            var person = this.getPersonByNick(nickname);
			this.ViewData.Model = new ProfileModel()
                { 
                    Person = person ?? new Candidate(),
                    EditControllerName = person.IsModel ? "Model" : "Candidate" 
                };

			if (person == null)
				this.TempData["Message"] = "Hai per caso provato a taroccare la URL???";

            #region AJAX
            if (Request.IsAjaxRequest())
                return this.View("ShowPerson", person);
            #endregion

            return this.View();
        }

        private Person getPersonByNick(string nickname)
        {
            IPersonRepository repository = this.personRepository;

            Person person = repository
                .Include("Multimedia").Include("NationalityCountry").Include("ResidenceCountry")
                .Where(c => c.NickName.ToLower() == nickname.ToLower()).SingleOrDefault();

            return person;
        }

        public ActionResult ShowPics(string nickname)
        {
            IPersonRepository repository = this.personRepository;

            Person person = repository
                .Include("Multimedia")
                .Where(c => c.NickName.ToLower() == nickname.ToLower()).SingleOrDefault();

            this.ViewData.Model = person;

            return this.View();
        }
    }
}